#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2010 - BidiX@BidiX.info
#
#
'''
    user - manage user, login and logout
    
    Description : Extend the gae users module
 
     Usage : 
         class User(google.appengine.api.userts.User)
         get_current_user()
         login_url(dset_url)
         logout_url(dest_url)
     
'''

from google.appengine.api import users

class User(users.User):
    '''
    Manage user and access 
    '''


    def __init__(self, guser):
        '''
        Constructor
        '''
        self.guser = guser
        
        
    def to_string(self):
        '''External representation'''
        return "%s (%s:%s:%s:%s)"%(self.nickname(), self.email(), self.user_id(), self.is_admin(), self.auth_domain())

    def auth_domain(self): 
        return self.guser.auth_domain()
    
    def email(self): 
        return self.guser.email()
    
    def is_admin(self):
        return users.is_current_user_admin()
 
    def nickname(self): 
        return self.guser.nickname()
 
    def user_id(self): 
        return self.guser.user_id()  

current_user = None

    
def get_current_user():
    global current_user
    u = users.get_current_user()
    if u:
        current_user = User(u)
        return current_user
    else:
        return None

def login_url(dest_url):
    return users.create_login_url(dest_url)

def logout_url(dest_url):
    return users.create_logout_url(dest_url)
        